import {
  baseUrl
} from '../../../config/api';

Page({
  data: {
    val:'',
    nameValue: '',
    userId: -1,
    type: -1,
    personInfo: {
      avatarUrl: '',
      nickName: '',
      gender: 0,
      phoneNumber: '',
      class: '',
      sNumber: '',
    },
  },
  onLoad(options) {
    console.log(options)
    this.setData({
      userId: options.id,
      type: options.type,
      val:options.val
    });
    wx.setNavigationBarTitle({
      title: options.type == 0 ? '修改昵称' : '修改手机号'
    })
  },
  onInput(event) {
    this.setData({
      nameValue: event.detail.value,
    });
  },
  onSubmit() {
    console.log(this.data)
    if (this.data.userId === -1) {
      return
    }
    wx.showLoading({
      title: '修改中...',
    })
    wx.request({
      url: baseUrl + 'sys/user/wx/update',
      method: 'post',
      data: {
        userId: this.data.userId,
        username: this.data.type == 0 ? this.data.val : null,
        mobile: this.data.type == 1 ? this.data.val : null,
      },
      success: (res) => {
        wx.hideLoading()
        if (res.data.code === 0) {
          wx.setStorage({
            key: "userInfo",
            data: JSON.stringify(res.data.data)
          })
          wx.showToast({
            title: '修改成功',
            icon: 'success',
            duration: 1000,
            success() {
              setTimeout(function () {
                wx.navigateBack()
              }, 1000)
            }
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'error',
            duration: 2000
          });
        }
      }
    })
  },
  clearContent() {
    this.setData({
      nameValue: '',
    });
  },
  init() {

  },
});